package top.lenjon.lib.exception;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import top.lenjon.lib.model.ResponseModel;

import java.io.IOException;
import java.sql.SQLException;
import java.util.stream.Collectors;

@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
///
//    /**
//     * 捕获数据库操作异常
//     */
//    @ExceptionHandler(value = SQLException.class)
//    public ResponseModel sQLExceptionHandler(SQLException e) {
//        logger.error("数据库操作异常:", e.getMessage());
//        return ResponseModel.error(e.getMessage());
//    }

    /**
     * 捕获数据校验异常
     */
    @ExceptionHandler(value = BindException.class)
    public ResponseModel bindExceptionHandler(BindException e) {
        logger.error("数据校验异常", e.getMessage());
        //改进一下返回信息。
        return ResponseModel.error(e.getMessage());
    }
///
//    /**
//     *捕获空指针异常
//     */
//    @ExceptionHandler(value = NullPointerException.class)
//    public ResponseModel nullPointExceptionHandler(NullPointerException e){
//        logger.error("空指针异常",e.getMessage());
//        return ResponseModel.error("空指针异常");
//    }

    /**
     * 捕获400异常
     */
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public ResponseModel httpMessageNotReadableExceptionHandler(HttpMessageNotReadableException e) {
        logger.error("错误请求", e.getMessage());
        return ResponseModel.error(e.getMessage());
    }

//    /**
//     * IO异常处理
//     */
//    @ExceptionHandler(value = IOException.class)
//    public ResponseModel iOExceptionHandler(IOException e) {
//        logger.error("读写异常", e.getMessage());
//        return ResponseModel.error(e.getMessage());
//    }

    /**
     * 权限认证
     */
    @ResponseStatus(code = HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(value = AuthException.class)
    public ResponseModel authExceptionHandler(AuthException e) {
        logger.error("没有权限", e);
        return ResponseModel.error(e.getMessage());
    }

    /**
     * valid校验
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResponseModel bingExceptionHandler(MethodArgumentNotValidException e) {
        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
        logger.error(message);
        return ResponseModel.error(message);
    }

}
